package com.khadi.testcase.view.workflow;

import info.clearthought.layout.TableLayout;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListModel;
import javax.swing.border.EtchedBorder;

import com.khadi.testcase.util.SessionUtil;
import com.khadi.testcase.util.UIHelper;
import com.khadi.testcase.view.ITestObserver;
import com.khadi.testcase.view.IWindowContent;
import com.khadi.testcase.view.editor.TextEditor;
import com.khadi.testcase.view.model.ExecuteTestCaseModel;
import com.khadi.testcase.view.model.TestCaseModel;
import com.khadi.testcase.view.model.TestCaseTableModel;
import com.khadi.testcase.view.model.WorkFlowModel;
import com.khadi.testcase.view.model.WorkflowDtlModel;
import com.khadi.testcase.view.renderer.RowHeaderRenderer;

public class DefineTestCasesTab implements IWindowContent {
	private WorkFlowModel model;
	private TestCaseTableModel testCaseTableModel;

	private ITestObserver observer;
	private String[] testCaseNos = { "Test Case No  : 1 ", "Test Case NO : 2 ",
			"Test Case No : 3 " };

	public DefineTestCasesTab(ITestObserver observer) {
		this.observer = observer;
	}

	@Override
	public Object getData() {

		List<TestCaseModel> testCaseList = testCaseTableModel.getData();

		List<ExecuteTestCaseModel> executeTestCaseList = new LinkedList<ExecuteTestCaseModel>();
		for (TestCaseModel testCaseModel : testCaseList) {
			ExecuteTestCaseModel executeTestCaseModel = new ExecuteTestCaseModel();
			executeTestCaseModel.setTestCaseModel(testCaseModel);
			executeTestCaseModel.setWorkFlowModel(model);
			executeTestCaseList.add(executeTestCaseModel);
		}

		return executeTestCaseList;
	}

	@Override
	public Component getView() {
		double[] columns = new double[] { -2, -1 };
		double[] rows = new double[] { -2, -1, -1 };

		TableLayout testCasesLayout = new TableLayout(columns, rows);
		testCasesLayout.setHGap(5);
		testCasesLayout.setVGap(5);

		JPanel testCasesPanel = new JPanel(testCasesLayout);

		testCasesPanel.setBorder(BorderFactory
				.createEtchedBorder(EtchedBorder.LOWERED));

		// -------------- Table

		ListModel lm = new AbstractListModel() {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public int getSize() {
				return testCaseNos.length;
			}

			public String getElementAt(int index) {
				return testCaseNos[index];
			}
		};

		testCaseTableModel = new TestCaseTableModel(getColums(), lm.getSize());

		JTable table = new JTable(testCaseTableModel);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setDefaultEditor(String.class, new TextEditor());
		table.setRowHeight(20);

		JList rowHeader = new JList(lm);

		rowHeader.setAutoscrolls(true);

		rowHeader.setFixedCellHeight(table.getRowHeight()
				+ table.getRowMargin());

		rowHeader.setCellRenderer(new RowHeaderRenderer(table));

		JScrollPane scroll = new JScrollPane(table);
		scroll.setRowHeaderView(rowHeader);

		// --------End table
		testCasesPanel.add(scroll, "0,0");

		JPanel nextPanel = new JPanel();
		nextPanel.setLayout(new BorderLayout(10, 10));
		JLabel lbNext = new JLabel(UIHelper
				.getImageIcon(UIHelper.IMG_RIGHT_ARROW_MARK));

		lbNext.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Map<String, Object> workFlowMap = (Map<String, Object>) SessionUtil
						.getInstance().getSessionMap().get(
								SessionUtil.WORK_FLOW);
				if (workFlowMap == null) {
					workFlowMap = new LinkedHashMap<String, Object>();
					SessionUtil.getInstance().setSessionObject(
							SessionUtil.WORK_FLOW, workFlowMap);
				}
				workFlowMap.put(model.getWorkflowName(), getData());

				// observer.notify(DefineTestCasesTab.this);
			}

		});

		nextPanel.add(lbNext, BorderLayout.EAST);
		testCasesPanel.add(nextPanel, "0,2");
		return testCasesPanel;
	}

	private List<TestCaseModel> getTestCaseData() {
		List<TestCaseModel> list = new LinkedList<TestCaseModel>();
		for (int i = 0; i < testCaseNos.length; i++) {
			TestCaseModel testCaseModel = new TestCaseModel();
			testCaseModel.setTestCaseNo(testCaseNos[i]);
			for (WorkflowDtlModel workflowDtlModel : model.getWorkflowDtlList()) {

				testCaseModel.setPropertyValue(workflowDtlModel
						.getDescription(), null);

			}
			list.add(testCaseModel);
		}
		return list;
	}

	private String[] getColums() {

		String[] columns = new String[model.getWorkflowDtlList().size()];
		for (int i = 0; i < columns.length; i++) {
			WorkflowDtlModel workflowDtlModel = model.getWorkflowDtlList().get(
					i);
			columns[i] = workflowDtlModel.getDescription();
		}
		return columns;
	}

	@Override
	public void postData(Object object) {
		testCaseTableModel.setData(getTestCaseData());
		//UIHelper.getWebDriver().quit();
	}

	@Override
	public void preData(Object object) {
		if (object != null) {
			model = (WorkFlowModel) object;
		}
	}
}
